package Entities; //done

import Client.ConnectionToServer;

/**
 * This class controls the Customer details and functions.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class Customer{

	/** Customer ID */
	private long CustID;
	
	/** Customer Name */
	private String CustName;
	
	/** Customer Age */
	private int CustAge;
	
	/** Customer AddressZone */
	private int CustAddressZone;
	
	/** Customer Phone */
	private String CustPhone;
	
	/** Customer importance */
	private int importance = 1;
	
	/**
	 * Default constructor
	 */
	public Customer(){
		this.CustID = 0;
		this.CustName = null;
		this.CustAge = 0;
		this.CustPhone = null;
		this.CustAddressZone = 0;
	}
	
	/**
	 * @param CustID the Customer ID 
	 * @param CustName the Customer Name
	 * @param CustAge the Customer Age
	 * @param CustPhone the Customer Phone
	 * @param CustAddressZone the Customer AddressZone
	 */
	public Customer(long CustID, String CustName, int CustAge,  int CustAddressZone, String CustPhone) {
		super();
		this.CustID = CustID;
		this.CustName = CustName;
		this.CustAge = CustAge;
		this.CustPhone = CustPhone;
		this.CustAddressZone = CustAddressZone;
	}

	/**
	 * @return the Customer ID
	 */
	public long getCustID() {
		return CustID;
	}
	
	/**
	 * @param CustID the customer id to set
	 */
	public void setCustID(long CustID) {
		this.CustID = CustID;
	}

	/**
	 * @return the Customer Name
	 */
	public String getCustName() {
		return CustName;
	}

	/**
	 * @param CustName the customer name to set
	 */
	public void setCustName(String CustName) {
		this.CustName = CustName;
	}

	/**
	 * @return the CustAge
	 */
	public int getCustAge() {
		return CustAge;
	}

	/**
	 * @param CustAge the customer age to set
	 */
	public void setCustAge(int CustAge) {
		this.CustAge = CustAge;
	}

	/**
	 * @return the Customer Phone
	 */
	public String getCustPhone() {
		return CustPhone;
	}

	/**
	 * @param CustPhone the customer phone to set
	 */
	public void setCustPhone(String CustPhone) {
		this.CustPhone = CustPhone;
	}

	/**
	 * @return the Customer AddressZone
	 */
	public int getCustAddressZone() {
		return CustAddressZone;
	}

	/**
	 * @param CustAddressZone the customer addressZone to set
	 */
	public void setCustAddressZone(int CustAddressZone) {
		this.CustAddressZone = CustAddressZone;
	}

	/**
	 * @return the importance
	 */
	public int getImportance() {
		return importance;
	}

	/**
	 * @param importance the importance to set
	 */
	public void setImportance(int importance) {
		this.importance = importance;
	}

	/**
	 * This function returns the Customer details as String Array
	 * 
	 */
	public String[] toTheArrString(){
		try{
			String[] customerarr = new String[5];
			customerarr[0] =  String.valueOf(getCustID());
			customerarr[1] = getCustName();
			customerarr[2] = String.valueOf(getCustAge());
			customerarr[3] = String.valueOf(getCustAddressZone());
			customerarr[4] = getCustPhone();
			return customerarr;
		}
		catch(Exception e){
			return null;
		}
	}
	
	/**
	 * This function updates customer details in the DB
	 * @return true on success , false on fail
	 */
	public String updateCustomer(){		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.updateCustomer(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function adds a new Customer to the DB
	 * @return true on success , false on fail
	 */
	public String addCustomer(){		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.addCustomer(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function loads customer details from DB 
	 * @param clientID the Client ID
	 * @return true on success , false on fail
	 */
	public boolean loadCustomer(long clientID){		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			String[] customer = (String[])connectiontoserver.loadCustomer(clientID);
			if (customer == null)
				return false;
			setCustID(Long.parseLong(customer[0]));
			setCustName(customer[1]);
			setCustAge(Integer.parseInt(customer[2]));
			setCustPhone(customer[3]);
			if (customer[4] == null)
				setCustAddressZone(0);
			else
				setCustAddressZone(Integer.parseInt(customer[4]));
			setImportance(Integer.parseInt(customer[5]));	
			return true;
		}
		catch(Exception e){
			return false;
		}				
	}
}
